package com.hqbzl.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j(topic = "TestNofity")
public class TestNofity2 {
    static final Object LOCK = new Object();
    static volatile boolean flag = true;
    static volatile int i = 0;

    public static void main1(String[] args) {

        final Thread t1 = new Thread(() -> {
            synchronized (LOCK) {
                while (true) {

                }
            }
        }, "t1");


        final Thread t2 = new Thread(() -> {
            synchronized (LOCK) {
                while (true) {
                    if (!flag) {
                        log.debug("t2 i = {}", i);
                        try {
                            LOCK.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        flag = true;
                    }

                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                }
            }
        }, "t2");

        t1.start();
        t2.start();
        System.out.println("main end");
    }

    public static void main(String[] args) {
        final Thread t1 = new Thread(() -> {
            synchronized (LOCK) {
                while (true) {
                    try {
                        log.debug("t1 wait");
                        TimeUnit.SECONDS.sleep(1);
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LOCK.notify();
                }
            }
        }, "t1");
        final Thread t2 = new Thread(() -> {
            synchronized (LOCK) {
                while (true) {
                    try {
                        log.debug("t2 wait");
                        TimeUnit.SECONDS.sleep(1);
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LOCK.notify();
                }
            }
        }, "t2");
        final Thread t3 = new Thread(() -> {
            synchronized (LOCK) {
                while (true) {
                    try {
                        log.debug("t3 wait");
                        TimeUnit.SECONDS.sleep(1);
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LOCK.notify();
                }
            }
        }, "t3");
        t1.start();
        t2.start();
        t3.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (LOCK) {
            LOCK.notify();
        }
    }
}
